package com.codebattery.service;

import com.codebattery.domain.entity.Node;
import com.codebattery.domain.model.*;
import com.codebattery.repository.MenuRepository;
import com.codebattery.repository.NodeRepository;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class NodeService {
    @Autowired
    private NodeRepository nodeRepository;

    public NodeInfo list(NodeQry qry){
        NodeInfo nodeInfo = new NodeInfo();

        List<Node> allNodes = Lists.newArrayList(nodeRepository.findAll());

        NodeTreeEle project = arr2tree(allNodes, "project");

        for(NodeTreeEle ele : project.getChildren()) {
            GroupInfo info = new GroupInfo();
            info.setNode(ele);
            info.getList().add(ele);
            nodeInfo.getGroups().put(ele.getKey(), info);
        }
        nodeInfo.setNodes(project.getChildren());

        return nodeInfo;
    }

    /**
     * 生成功能树
     * 
     * @param nodes
     * @return
     */
    private NodeTreeEle arr2tree(List<Node> nodes, String node) {

        NodeTreeEle tree = new NodeTreeEle(node);

        List<NodeTreeEle> allNodeTreeEles = new LinkedList<>();
        
        for(Node n : nodes) {
            String[] nodeStrs = n.getNode().split("/");

            NodeTreeEle topTreeEle = null;
            NodeTreeEle parentTreeEle = null;
            for(int i=0; i<nodeStrs.length; i++) {

                String str = nodeStrs[i];

                NodeTreeEle ele = null;

                if(parentTreeEle!=null) {
                    ele = treeContain(allNodeTreeEles, parentTreeEle.getKey() + "/" + str);
                } else {
                    ele = treeContain(allNodeTreeEles, str);
                }

                if(ele==null){
                    ele = new NodeTreeEle(str);

                    allNodeTreeEles.add(ele);
                }
                
                if(parentTreeEle!=null){
                    if(treeContain(parentTreeEle.getChildren(), parentTreeEle.getKey() + "/" + str) == null){
                        parentTreeEle.getChildren().add(ele);
                    }
                    ele.setKey(parentTreeEle.getKey() + "/" + str);
                }

                if(i==0) {
                    topTreeEle = ele;
                }

                if(i==nodeStrs.length-1){
                    ele.setId(n.getId());
                    ele.setNode(ele.getKey());
                    ele.setIsAuth(n.getIsAuth());
                    ele.setIsLogin(n.getIsLogin());
                    ele.setIsMenu(n.getIsMenu());
                    ele.setTitle(n.getTitle());
                }

                parentTreeEle = ele;
            }

            if(treeContain(tree.getChildren(), topTreeEle.getKey()) == null){
                tree.getChildren().add(topTreeEle);
            }
        }

        return tree;
    }
    
    
    
    private NodeTreeEle treeContain(List<NodeTreeEle> list, String nodeKey){
        NodeTreeEle result = null;
        
        for(NodeTreeEle ele : list) {
            if(ele.getKey().equals(nodeKey)){
                result = ele;
                break;
            }
        }
        
        return result;
    }

}
